<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Focus;
use App\Models\HoneyBalance;
use App\Models\User;
use App\Services\HoneyBalanceService;
use Dingo\Api\Http\Request;

class UserController extends BaseController
{

    /**
     * 用户列表
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request) {
        $title = $request->input('title');
        $fields = ['id','nickname','avatar_url', 'skill', 'desc', 'city'];
        $user_id = $this->getUserId();
        $list = User::select($fields)->when($title, function ($q) use($title){
            $q->where('nickname', 'like', '%' . $title . '%');
        })->userFocus($user_id)->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }

    public function index(Request $request)
    {
        $this->validate($request, [
           'user_id' => 'required|exists:users,id'
        ]);
        $id = \request('user_id');
        $info = User::withCount(['is_focus' => function($q) {
            $user_id = $this->getUserId();
            $q->where('user_id', $user_id);
        }])->find($id);

        if ($id != $this->getUserId()) {
            $info->setHidden(['balance', 'mobile', 'openid', 'honey_balance', 'union_id', 'business_license', 'credit_code', 'compnay_name', 'realname',
                'id_card', 'facade_pic', 'back_pic']);
        }
        return $this->success($info);
    }

    public function fans(Request $request) {
        $this->validate($request, [
            'user_id' => 'required'
        ]);
        $cur_user_id = $this->getUserId();
        $id = \request('user_id');
        $list = Focus::where('target_user_id', $id)->withCertain('fans', ['nickname', 'avatar_url'])->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        foreach ($list as &$item) {

            //判断我是否关注他
            $exist = Focus::where('user_id', $cur_user_id)->where('target_user_id', $item->user_id)->exists();
            $item->is_focus = $exist ? 1 : 0;

        }
        return $this->paginate($list);
    }

    public function focus(Request $request) {
        $this->validate($request, [
            'user_id' => 'required'
        ]);
        $cur_user_id = $this->getUserId();
        $id = \request('user_id');
        $list = Focus::where('user_id', $id)->withCertain('focus', ['nickname', 'avatar_url'])->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        foreach ($list as &$item) {
            //判断我是否关注他
            $exist = Focus::where('user_id', $cur_user_id)->where('target_user_id', $item->target_user_id)->exists();
            $item->is_focus = $exist ? 1 : 0;
        }
        return $this->paginate($list);
    }

    public function store(Request $request) {
        $user = $this->getUser();

        $input = $request->only(['avatar_url', 'nickname', 'city', 'desc', 'skill', 'mobile']);
        $input = array_filter($input);
        $user->update($input);
        if ($user->avatar_url && $user->nickname && $user->city && $user->desc && $user->skill && $user->mobile) {
            $exist = HoneyBalance::where('user_id', $user->id)->where('type', HoneyBalance::TYPE_PROFILE)->exists();
            if (!$exist) {
                HoneyBalanceService::store($user->id, HoneyBalance::POINT_PROFILE, HoneyBalance::TYPE_PROFILE, 0, true);
            }
        }
        return $this->success([]);
    }

    public function identify(Request $request) {
        $user = $this->getUser();
        $input = $request->only(['realname', 'id_card', 'facade_pic', 'back_pic', 'type', 'company_name', 'credit_code', 'business_license']);
        $input['audit_status'] = 1;
        $user->update($input);
        return $this->success([]);
    }

}

